using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using backend.Data;
using backend.Models;
using backend.Models.Dtos;
using Microsoft.EntityFrameworkCore;

namespace backend.Repositories
{
    public class ZoneRepo : BaseRepo<CampusFacilityRepairManagementDbContext, FirstClass>
    {
        private readonly IMapper _mapper;
        private readonly CampusFacilityRepairManagementDbContext _context;

        public ZoneRepo(CampusFacilityRepairManagementDbContext context, IMapper mapper) : base(context)
        {
            _mapper = mapper;
            _context = context;
        }

        public async Task<FirstClass> CreateFirstClassAsync(string ID, string firstClassName)
        {
            // Check if there's an existing entry with the same name but marked as stopped using.
            var existingFirstClass = await _context.FirstClasses.FirstOrDefaultAsync(fc => fc.FcName == firstClassName && fc.FcStopUsing);

            if (existingFirstClass != null)
            {
                // If found, reactivate it by setting FcStopUsing to false.
                existingFirstClass.FcStopUsing = false;
                await _context.SaveChangesAsync();
                return existingFirstClass;
            }
            else
            {
                var firstClass = new FirstClass { FcId = ID, FcName = firstClassName };
                // Otherwise, add the new entry.
                await _context.FirstClasses.AddAsync(firstClass);
                await _context.SaveChangesAsync();
                return firstClass;
            }
        }

        public async Task<bool> DeleteFirstClassAsync(string fcId)
        {
            var firstClass = await _context.FirstClasses.FindAsync(fcId);
            if (firstClass != null)
            {
                // Instead of removing the entity, mark it as not in use.
                firstClass.FcStopUsing = true;
                await _context.SaveChangesAsync();
                return true;
            }
            return false;
        }

        public async Task<SecondClass> CreateSecondClassAsync(string scid, string secondClassName, string firstClassId)
        {
            if (string.IsNullOrEmpty(firstClassId))
            {
                throw new ArgumentException("First class id cannot be null or empty.");
            }
            var secondClass = new SecondClass { ScId = scid, ScName = secondClassName, FcId = firstClassId };
            await _context.SecondClasses.AddAsync(secondClass);
            await SaveChangesAsync();
            return secondClass;
        }

        public async Task<bool> DeleteSecondClassAsync(string scId)
        {
            var secondClass = await _context.SecondClasses.FindAsync(scId);
            if (secondClass != null)
            {
                _context.SecondClasses.Remove(secondClass);
                await SaveChangesAsync();
                return true;
            }
            return false;
        }

        public async Task<List<ZoneDto>> GetZonesAsync()
        {
            var firstClasses = await _context.FirstClasses
                .Where(fc => !fc.FcStopUsing)
                .Include(fc => fc.SecondClasses)
                .ToListAsync();

            var zones = firstClasses.Select(fc => new ZoneDto
            {
                Value = fc.FcId,
                Label = fc.FcName,
                Children = fc.SecondClasses
                    .Select(sc => new ZoneDto
                    {
                        Value = sc.ScId,
                        Label = sc.ScName,
                        Children = new List<ZoneDto>() // Assuming no further nesting for simplicity
                    })
                    .ToList()
            }).ToList();

            return zones;
        }

        public async Task SaveChangesAsync()
        {
            await _context.SaveChangesAsync();
        }
    }
}